package com.zwj.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.common.R;
import com.zwj.common.entity.SchoolAdministrator;
import com.zwj.user.entity.UserAndRole;
import com.zwj.user.entity.UserId;
import com.zwj.user.security.PasswordSecurity;
import com.zwj.user.service.SchoolAdministratorService;
import com.zwj.user.mapper.SchoolAdministratorMapper;
import com.zwj.user.service.UserAndRoleService;
import com.zwj.user.service.UserIdService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
* @author 23088
* @description 针对表【school_administrator(学校管理员)】的数据库操作Service实现
* @createDate 2022-10-20 23:28:14
*/
@Service
public class SchoolAdministratorServiceImpl extends ServiceImpl<SchoolAdministratorMapper, SchoolAdministrator>
    implements SchoolAdministratorService{

    @Resource
    private UserIdService userIdService;

    @Resource
    private UserAndRoleService userAndRoleService;

    @Override
    @Transactional
    public String register(SchoolAdministrator schoolAdministrator) {

        UserId userId = new UserId(200);
        userIdService.save(userId);
        schoolAdministrator.setId(userId.getUserId());

        //MD5加密
        String password = schoolAdministrator.getPassword();
        String md5Password = PasswordSecurity.Md5Encoding(password);
        schoolAdministrator.setPassword(md5Password);

        schoolAdministrator.setRegisterTime(LocalDateTime.now());
        schoolAdministrator.setUpdateTime(LocalDateTime.now());

        boolean save = this.save(schoolAdministrator);

        //用户与角色绑定
        UserAndRole userAndRole = new UserAndRole();
        userAndRole.setUserId(schoolAdministrator.getId());
        userAndRole.setRoleId(3L);
        userAndRole.setCreateTime(LocalDateTime.now());

        userAndRoleService.save(userAndRole);

        if(!save)
            return "错误!";

        return "注册成功!";
    }
}




